<template>
    <div>
        <!--顶部滑动条-->
        <div id="slider" class="mui-slider" style="border-bottom: 1px solid #007aff">
            <div id="sliderSegmentedControl" class="mui-scroll-wrapper mui-slider-indicator mui-segmented-control mui-segmented-control-inverted">
                <div class="mui-scroll">
                    <a :class="['mui-control-item', item.id == 0 ? 'mui-active' : '']"
                                 v-for="(item,index) in category" :key="item.id" @click="getPhotoList(item.id)">
                        {{item.title}}
                    </a>
                </div>
            </div>
        </div>
        <!--图片列表-->
        <ul class="photo-list">
            <router-link :to="'/home/photoInfo/'+imgItem.id" v-for="(imgItem,index) in photoList"
                         :key="index" tag="li">
                <img v-lazy="imgItem.img_url">
                <div class="photo-info">
                    {{imgItem.title}}
                </div>
            </router-link>
        </ul>
    </div>
</template>
<script>

    //1.导入mui的js文件
    import mui from '../../lib/mui/js/mui.js'

    //导入mui 会报错' Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them'
    //但webpack打包好的bundle.js中默认启用了严格默认，所以两个冲突了
    //解决方法：把webpack打包时的严格默认禁用掉


    export default{
        data(){
            return {
                category:[],
                photoList:[],
            }
        },
        created(){
            this.getCategory();
            this.getPhotoList(0);
        },
        mounted(){
            //2.初始化页面完成后 再 初始化滑动控件，这样就解决了一进到这个页面滑动不了的问题
            mui('.mui-scroll-wrapper').scroll({
                deceleration:0.0005  // flick 减速系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
            });
        },
        methods: {
            //获取所有分类
            getCategory(){
                this.$http.get('src/mock/category.json').then(result=>{
                    if (result.status == 200){
                        result.body.unshift({title:"全部",id:0});
                        this.category = result.body;
                    }
                })
            },
            getPhotoList(showId){
                this.$http.get('src/mock/photoList.json').then(result=>{
                        if (result.status == 200){
                            if(showId == 0){
                                this.photoList = result.body;
                            }
                            if(showId == 1){
                                this.photoList = result.body.slice(0,1);
                            }
                            if(showId == 2){
                                this.photoList = result.body.slice(1,2);
                            }
                            if(showId == 3){
                                this.photoList = result.body.slice(2,3);
                            }
                            if(showId == 4){
                                this.photoList = result.body.slice(3,4);
                            }
                            if(showId == 5){
                                this.photoList = result.body.slice(4,5);
                            }

                        }
                })

            }
        },
        computed: {},
        components: {}
    }
</script>
<style lang="scss" scoped>
    /*解决报错：[Intervention] Unable to preventDefault inside passive event listener due to target being treated as passive. See https://www.chromestatus.com/features/5093566007214080*/
    *{
        touch-action: none;
    }
    .mui-segmented-control.mui-scroll-wrapper .mui-control-item {
        padding: 0 22px;
    }
    .photo-list{
        list-style: none;
        margin: 0;
        padding: 15px 25px;
        background: #CCCCCC;
        li{
            text-align: center;
            margin-bottom: 10px;
            position: relative;
            overflow: hidden;
            background: #ffffff;
            img{
                width: 55%;
                margin: 20px auto;
                vertical-align: middle;
            }
            .photo-info{
                width: 100%;
                font-size: 20px;
                color: #ffffff;
                text-align: center;
                height: 50px;
                line-height: 50px;
                background: rgba(0,0,0,0.4);
                position: absolute;
                bottom: 0;
                padding-left: 10px;
                box-sizing: border-box;
                font-weight: bold;
            }
            img[lazy=loading]{
                width: 40px;
                height: 300px;
                margin: auto;
            }
        }
    }
</style>